#!/usr/bin/env python3
"""
获取配置中的CDN域名
"""

import json
import sys
import os

def get_cdn_domain(config_file='config.json'):
    """从配置文件获取CDN域名"""
    try:
        script_dir = os.path.dirname(os.path.abspath(__file__))
        config_path = os.path.join(script_dir, config_file)

        if not os.path.exists(config_path):
            print(f"配置文件不存在: {config_path}", file=sys.stderr)
            return None
            
        with open(config_path, 'r', encoding='utf-8') as f:
            config = json.load(f)
        
        cdn_config = config.get('cdn', {})
        if not cdn_config.get('enable'):
            print("CDN功能未启用", file=sys.stderr)
            return None

        cdn_domain = cdn_config.get('domain')
        if not cdn_domain:
            print("未配置CDN域名", file=sys.stderr)
            return None

        protocol = cdn_config.get('protocol', 'https')
        return f"{protocol}://{cdn_domain}"

    except Exception as e:
        print(f"读取配置文件失败: {e}", file=sys.stderr)
        return None

if __name__ == "__main__":
    domain = get_cdn_domain()
    if domain:
        print(domain)
    else:
        sys.exit(1)
